package com.arlen.common.conf;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zaxxer.hikari.HikariDataSource;
import lombok.Getter;
import lombok.Setter;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;
/**
 * @Author Arlen
 * @Description 配置数据库连接
 * @Date 9:38 下午 2021/4/25
 * @return
        **/
@Getter
@Setter
@Configuration
@ConfigurationProperties(prefix="mysql.core")
@PropertySource("classpath:mysql-core-jdbc.properties")
public class MybatisConf {
    String jdbcUrl;
    String jdbcUserName;
    String jdbcPassword;
    //String jdbcDriver;
    //String rootMapper;//mapper文件在classpath下存放的根路径
    //String aliasesPackage;//别名包

    //配置连接池
    @Bean("HikariDataSource")
    public DataSource mysqlCoreDataSource(){
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setUsername(this.getJdbcUserName());
        dataSource.setPassword(this.getJdbcPassword());
        dataSource.setJdbcUrl(this.getJdbcUrl());
        dataSource.setDriverClassName(DatabaseDriver.MYSQL.getDriverClassName());//一定要和枚举类中写的一样
        //最大连接数
        dataSource.setMaximumPoolSize(500);
        //最小连接数
        dataSource.setMinimumIdle(10);
        return dataSource;
    }

    @Bean("SqlSessionFactoryBean")
    public MybatisSqlSessionFactoryBean getSqlSessionFactoryBean(@Qualifier("HikariDataSource")DataSource dataSource){
        MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        return sessionFactoryBean;
    }

    //配置分页拦截器
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return mybatisPlusInterceptor;
    }








}
